﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shop.Model;
using Shop.Model.Custom;
using System.Data.Linq;

namespace Shop.Framework.Services {
    public class CustomerService:BaseService {

        public bool Login(string username, string password) {
            ShopDatabaseDataContext context = Context;
            Customer customer = context.Customers.FirstOrDefault(cust => cust.LoginId == username && cust.Password == password);

            if (customer != null) {
                WebCart.Self.CurrentCustomer = customer;
                return true;
            } else
                return false;
        }

        public void RegisterNew(Customer customer) {
            try {
                ShopDatabaseDataContext context = Context;
                var found = context.Customers.FirstOrDefault(cust => cust.LoginId == customer.LoginId);
                if (found != null)
                    throw new Exception("The user login already exists..");

                context.Customers.InsertOnSubmit(customer);
                context.SubmitChanges();
            } catch (Exception ex) {
                throw ex;
            }
        }

        public void EditProfile(Customer customer) {
            throw new NotImplementedException();
        }

        public List<CustomerOrder> GetCustomerOrders(DateTime to, DateTime fromDate) {
            try {
                ShopDatabaseDataContext context = Context;
                DataLoadOptions option = new DataLoadOptions();
                option.LoadWith<CustomerOrder>(prd => prd.Customer);
                context.LoadOptions = option;
                var found = from i in context.CustomerOrders
                            where i.DateOrdered >= to && i.DateOrdered <= fromDate
                            select i;

                return found.ToList();
                
            } catch (Exception ex) {
                throw ex;
            }
        }

        public bool SetCompleted(int p) {
            try {
                ShopDatabaseDataContext context = Context;
                var order = context.CustomerOrders.FirstOrDefault(custO => custO.IdCustomerOrder == p);
                order.Completed = true;
                context.SubmitChanges();

            } catch (Exception ex) {
                throw ex;
            }

            return true;
        }

        public CustomerOrder GetCustomerOrders(int idCustomerOrder) {
            try {
                ShopDatabaseDataContext context = Context;
                DataLoadOptions option = new DataLoadOptions();
                option.LoadWith<CustomerOrder>(prd => prd.Customer);
                option.LoadWith<CustomerOrder>(prd => prd.OrderItems);
                context.LoadOptions = option;

                var res = context.CustomerOrders.FirstOrDefault(c => c.IdCustomerOrder == idCustomerOrder);

                return res;
            } catch (Exception ex) {
                throw ex;
            }
        }
    }
}
